package crmdna.client.practice;

import java.util.List;

import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.practice.Practice.PracticeProp;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.crmdna.Client;

@Api(name = "practice")
public class PracticeApi {
	@ApiMethod(path="create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client, 
			@Named("displayName") String displayName, User user) {
		
		try {
			Client.ensureValidClient(client);
			PracticeProp prop = Practice.create(client, displayName, Utils.getLoginEmail(user));
								
			return new APIResponse(Status.SUCCESS, prop);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Named("client") String client) {
		
		try {						
			Client.ensureValidClient(client);
			List<PracticeProp> props = Practice.getAll(client);
				
			return new APIResponse(Status.SUCCESS, props);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="rename", httpMethod = HttpMethod.GET)
	public APIResponse rename(@Named("client") String client, 
			@Named("practiceId") long practiceId, 
			@Named("newDisplayName") String newDisplayName, User user) {
		
		try {
			Client.ensureValidClient(client);
			PracticeProp prop = Practice.rename(client, practiceId, newDisplayName, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, prop);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Named("client") String client, 
			@Named("practiceId") long practiceId, User user) {
		
		try {
			Client.ensureValidClient(client);
			Practice.delete(client, practiceId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, "Center [" + practiceId + "] deleted");			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}		
}
